﻿using ThresholdAssistant.Autofac;
using ThresholdAssistant.DataContract.Result;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ThresholdAssistant.Http
{
    /// <summary>
    /// Polly客户端工具类
    /// </summary>
    public class PollyClientUtil : IBaseAutofac
    {
        /// <summary>
        /// 
        /// </summary>
        public PollyClientHelp PollyClientHelp { get; set; }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult> GetAsync(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            return await PollyClientHelp.GetAsync<ContractResult<string>>(url, paramters, headers, timeoutSeconds);
        }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult<T>> GetAsync<T>(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            return await PollyClientHelp.GetAsync<ContractResult<T>>(url, paramters, headers, timeoutSeconds);
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult> PostAsync<R>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            return await PollyClientHelp.PostAsync<R, ContractResult>(url, paramters, headers, contentType, timeoutSeconds);
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult<T>> PostAsync<T>(string url, string paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            return await PollyClientHelp.PostAsync<ContractResult<T>>(url, paramters, headers, contentType, timeoutSeconds);
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<ContractResult<T>> PostAsync<R, T>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            return await PollyClientHelp.PostAsync<R, ContractResult<T>>(url, paramters, headers, contentType, timeoutSeconds);
        }
    }
}